package com.task;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author 17930
 */
public class FTPUploader {
   private static final int PORT = 21; // FTP服务器端口
   public static   void uploader(String SERVER, String USERNAME, String PASSWORD, ArrayList<String> list) {
      FTPClient ftpClient = new FTPClient();
      try {
         ftpClient.connect(SERVER, PORT);
         ftpClient.login(USERNAME, PASSWORD);
         ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
         ftpClient.setControlEncoding("UTF-8");
         ftpClient.enterLocalPassiveMode();
         System.out.println(ftpClient.getReplyCode());
         for (String list1 : list) {
            File file = new File(list1);
            String remoteFile = file.getName();
            FileInputStream inputStream = new FileInputStream(file);
            boolean b = ftpClient.storeFile(remoteFile, inputStream);
            if (b){
               System.out.println("上传成功");

            }else {
               System.out.println("上传失败");
            }
         }
      } catch (IOException e) {
         e.printStackTrace();
      } finally {
         try {
            if (ftpClient.isConnected()) {
               ftpClient.logout();
               ftpClient.disconnect();
            }
         } catch (IOException e) {
            e.printStackTrace();
         }
      }
   }
}